/*
 * 
 * 
 * 
 */
package org.jjliu.example.dao;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.jjliu.example.dao.Order.Direction;

/**
 * 分页结果&查询条件
 * 
 * 
 * 
 */
public class PageModel implements Serializable {

	private static final long serialVersionUID = -3930180379790344299L;

	/** 默认页码 */
	private static final int DEFAULT_PAGE_NUMBER = 1;

	/** 默认每页记录数 */
	private static final int DEFAULT_PAGE_SIZE = 20;
	
	/** 最大每页记录数 */
	private static final int MAX_PAGE_SIZE = 2000;

	//设置值
	/** 页码 */
	private int pageNumber = DEFAULT_PAGE_NUMBER;

	/** 每页记录数 */
	private int pageSize = DEFAULT_PAGE_SIZE;

	/** 搜索属性 */
	private String searchProperty;

	/** 搜索值 */
	private String searchValue;

	/** 排序属性 */
	private String orderProperty;

	/** 排序方向 */
	private Direction orderDirection;

	
//返回值
	
	/** 内容 */
	private List<?> content;

	/** 总记录数 */
	private long total;

	
	/**
	 * 初始化一个新创建的Pageable对象
	 */
	public PageModel() {
		content = new ArrayList<Object>();
	}
	/**
	 * 禁止分页
	 * @return
	 */
	public PageModel forbidPage(){
		pageSize = Integer.MAX_VALUE;
		return this;
	}
	/**
	 * 检查是否允许分页
	 * @return
	 */
	public boolean isForbidPage(){
		return (pageSize == Integer.MAX_VALUE);
	}
	
	/**
	 * 获取页码
	 * 
	 * @return 页码
	 */
	public int getPageNumber() {
		return pageNumber;
	}

	/**
	 * 设置页码
	 * 
	 * @param pageNumber
	 *            页码
	 */
	public PageModel setPageNumber(int pageNumber) {
		if (pageNumber < 1) {
			pageNumber = DEFAULT_PAGE_NUMBER;
		}
		this.pageNumber = pageNumber;
		return this;
	}
	/**
	 * 获取起始index
	 * 
	 * @return 页码
	 */
	public int getFirstResult() {
		return (pageNumber-1)*pageSize;
	}

	/**
	 * 获取每页记录数
	 * 
	 * @return 每页记录数
	 */
	public int getPageSize() {
		return pageSize;
	}

	/**
	 * 设置每页记录数
	 * 
	 * @param pageSize
	 *            每页记录数
	 */
	public PageModel setPageSize(int pageSize) {
		if (pageSize < 1 || pageSize > MAX_PAGE_SIZE) {
			pageSize = DEFAULT_PAGE_SIZE;
		}
		this.pageSize = pageSize;
		return this;
	}

	/**
	 * 获取搜索属性
	 * 
	 * @return 搜索属性
	 */
	public String getSearchProperty() {
		return searchProperty;
	}

	/**
	 * 设置搜索属性
	 * 
	 * @param searchProperty
	 *            搜索属性
	 */
	public PageModel setSearchProperty(String searchProperty) {
		this.searchProperty = searchProperty;
		return this;
	}

	/**
	 * 获取搜索值
	 * 
	 * @return 搜索值
	 */
	public String getSearchValue() {
		return searchValue;
	}

	/**
	 * 设置搜索值
	 * 
	 * @param searchValue
	 *            搜索值
	 */
	public PageModel setSearchValue(String searchValue) {
		this.searchValue = searchValue;
		return this;
	}

	/**
	 * 获取排序属性
	 * 
	 * @return 排序属性
	 */
	public String getOrderProperty() {
		return orderProperty;
	}

	/**
	 * 设置排序属性
	 * 
	 * @param orderProperty
	 *            排序属性
	 */
	public PageModel setOrderProperty(String orderProperty) {
		this.orderProperty = orderProperty;
		return this;
	}

	/**
	 * 获取排序方向
	 * 
	 * @return 排序方向
	 */
	public Direction getOrderDirection() {
		return orderDirection;
	}

	/**
	 * 设置排序方向
	 * 
	 * @param orderDirection
	 *            排序方向
	 */
	public PageModel setOrderDirection(Direction orderDirection) {
		this.orderDirection = orderDirection;
		return this;
	}

	
	/**
	 * 获取总记录数
	 * 
	 * @return 总记录数
	 */
	public long getTotal() {
		return total;
	}
	/**
	 * 设置总记录数
	 * @param total
	 */
	public void setTotal(long total) {
		this.total = total;
	}

	/**
	 * 获取查询内容
	 * @return
	 */
	public List<?> getContent() {
		return content;
	}

	/**
	 * 设置查询内容
	 * @param content
	 */
	public void setContent(List<?> content) {
		this.content = content;
	}




	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		PageModel other = (PageModel) obj;
		if (content == null) {
			if (other.content != null)
				return false;
		} else if (!content.equals(other.content))
			return false;
		if (orderDirection != other.orderDirection)
			return false;
		if (orderProperty == null) {
			if (other.orderProperty != null)
				return false;
		} else if (!orderProperty.equals(other.orderProperty))
			return false;
		if (pageNumber != other.pageNumber)
			return false;
		if (pageSize != other.pageSize)
			return false;
		if (searchProperty == null) {
			if (other.searchProperty != null)
				return false;
		} else if (!searchProperty.equals(other.searchProperty))
			return false;
		if (searchValue == null) {
			if (other.searchValue != null)
				return false;
		} else if (!searchValue.equals(other.searchValue))
			return false;
		if (total != other.total)
			return false;
		return true;
	}
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((content == null) ? 0 : content.hashCode());
		result = prime * result
				+ ((orderDirection == null) ? 0 : orderDirection.hashCode());
		result = prime * result
				+ ((orderProperty == null) ? 0 : orderProperty.hashCode());
		result = prime * result + pageNumber;
		result = prime * result + pageSize;
		result = prime * result
				+ ((searchProperty == null) ? 0 : searchProperty.hashCode());
		result = prime * result
				+ ((searchValue == null) ? 0 : searchValue.hashCode());
		result = prime * result + (int) (total ^ (total >>> 32));
		return result;
	}

}